extends TextureRect
class_name Card

# 显示费用的标签
@export var CostLabel: Label
@export var DesLabel: Label

# 记录卡牌的状态，用于鼠标操作时，判断如何反应
enum STATUS {IDLE, HOLD, BLOCK, TRIGGER}
var my_status

const ANIM_TICK = 0.2

var my_index

var my_player

var base_pos
var base_scale

var mouse_bias_start
var deck_path

var my_cost = 1

const TRIGGER_Y = 400

func _ready():
	my_status = STATUS.IDLE
	deck_path = get_parent().get_parent()
	update_cost(my_cost)
	call_deferred("after_ready")
	print("card ready")

# 鼠标移入时，设定动画
func _on_mouse_entered():
	if Chessboard.board_state == Chessboard.STATE.PLAYER_MOVE:
		if my_status == STATUS.IDLE and Chessboard.focus_card == null and my_player == Chessboard.focus_player:
			# 移动到上层来，避免被其他牌遮蔽
			z_index = 1
			var tween = get_tree().create_tween()
			tween.tween_property(self, "position", Vector2(base_pos.x, base_pos.y - 140), ANIM_TICK)
			tween.parallel().tween_property(self, "scale", Vector2(1.3 * base_scale.x, 1.3 * base_scale.y), ANIM_TICK)
			# 卡片方向改为垂直
			tween.parallel().tween_property(get_parent(), "rotates", false, ANIM_TICK)
			tween.parallel().tween_property(get_parent(), "rotation", 0, ANIM_TICK)
			deck_path.card_enter_rearrange_for(my_index)

# 鼠标移出时，设定动画
func _on_mouse_exited():
	if my_status == STATUS.IDLE and Chessboard.focus_card == null:
		reset_anim_play()
		deck_path.card_reset_arrange()

# 接收玩家输入
func _gui_input(event):
	# 鼠标左键点击和释放
	if (event is InputEventMouseButton) and (event.button_index == 1):
		if event.button_mask == 1:
			on_click_left()
		elif event.button_mask == 0:
			pass
	# 鼠标右键点击和释放
	elif (event is InputEventMouseButton) and (event.button_index == 2):
		if event.button_mask == 2:
			on_click_right()
		elif event.button_mask == 0:
			pass

# 卡牌被左键点击时，开始跟随鼠标
func on_click_left():
	if Chessboard.board_state == Chessboard.STATE.PLAYER_MOVE and my_player == Chessboard.focus_player:
	# 确保此时没有别的卡被拿起来
		if my_status == STATUS.IDLE and Chessboard.focus_card == null:
			Chessboard.board_state = Chessboard.STATE.CARD_ACTION
			# 移动到上层
			z_index = 1
			my_status = STATUS.HOLD
			Chessboard.focus_card = self
			# 获取与鼠标位置的偏移量
			mouse_bias_start = position - get_global_mouse_position()
			var tween = get_tree().create_tween()
			tween.tween_property(self, "scale", Vector2(1.15 * base_scale.x, 1.15 * base_scale.y), ANIM_TICK / 2.0)
			tween.tween_property(self, "scale", Vector2(1.3 * base_scale.x, 1.3 * base_scale.y), ANIM_TICK / 2.0)

# 右键点击时
func on_click_right():
	if Chessboard.board_state == Chessboard.STATE.CARD_ACTION:
		if my_status == STATUS.HOLD or my_status == STATUS.TRIGGER:
			Chessboard.board_state = Chessboard.STATE.PLAYER_MOVE
			# 恢复层级
			z_index = 0
			my_status = STATUS.IDLE
			# 释放占用的card holder
			Chessboard.focus_card = null
			reset_anim_play()
			deck_path.card_reset_arrange()

func _process(_delta):
	# 卡牌被持有时，
	if my_status == STATUS.HOLD:
		# 跟随鼠标
		position = get_global_mouse_position() + mouse_bias_start
		# 移动y轴阈值后触发打出
		if global_position.y < TRIGGER_Y:
			my_status = STATUS.TRIGGER
			# 设定动画
			var tween = get_tree().create_tween()
			tween.parallel().tween_property(self, "global_position", Vector2(1000, 300), ANIM_TICK)

# 初始化完成后，获取基准位置和缩放大小，用于动画reset
func after_ready():
	base_pos = position
	base_scale = scale

# 恢复原状的动画
func reset_anim_play():
	z_index = 0
	var tween = get_tree().create_tween()
	tween.parallel().tween_property(self, "position", Vector2(base_pos.x, base_pos.y), ANIM_TICK)
	tween.parallel().tween_property(self, "scale", Vector2(base_scale.x, base_scale.y), ANIM_TICK)
	# 方向变回符合扇形轨道
	tween.parallel().tween_property(get_parent(), "rotates", true, ANIM_TICK)

# 卡牌打出
func try_play_this_card(coord_in):
	if my_cost > Chessboard.focus_player.left_cost:
		# 费用不足时，自动执行右键取消
		on_click_right()
		print("no enough cost")
	else:
		# 触发卡牌效果
		if activate_card_effect(coord_in): # 判断是否执行成功，如果选了无效的目标等情况时，会失败
			# 移动到强调位
			my_status = STATUS.BLOCK
			# var tween = get_tree().create_tween()
			# tween.tween_property(self, "position", Vector2(base_pos.x, base_pos.y - 300), ANIM_TICK * 2)
			Chessboard.focus_player.update_cost(Chessboard.focus_player.left_cost - my_cost)
			# 由deck来弃牌
			deck_path.discard_card(my_index)
			# 释放占有的holder
			Chessboard.focus_card = null
			# 清除自身
			Chessboard.board_state = Chessboard.STATE.PLAYER_MOVE
			# 状态恢复，以防下次抽到时显示问题
			position = base_pos
			scale = base_scale
			my_status = STATUS.IDLE

# 触发卡牌效果，之前已经进行了费用检查
func activate_card_effect(target_coord):
	print("card target:", target_coord)
	return true

# 更新费用显示label
func update_cost(target_cost):
	my_cost = target_cost
	CostLabel.text = str(my_cost)

# 更新卡牌文本描述，在某些数值修改时，需要调用
func update_des(des_in):
	DesLabel.text = des_in
